/**
 */
package com.dingpeikeji.quickdeer.core.modules.trip.web;

import java.text.DecimalFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.utils.StringUtils;
import com.dingpeikeji.quickdeer.core.common.web.BaseController;
import com.dingpeikeji.quickdeer.core.modules.trip.entity.MealsInfo;
import com.dingpeikeji.quickdeer.mobile.entity.CitySceneryInfo;
import com.dingpeikeji.quickdeer.mobile.entity.DestinationInfo;
import com.dingpeikeji.quickdeer.mobile.entity.TripCounselorInfo;
import com.dingpeikeji.quickdeer.mobile.service.CitySceneryService;
import com.dingpeikeji.quickdeer.mobile.service.DestinationService;
import com.dingpeikeji.quickdeer.mobile.service.TripCounselorService;


/**
 * 行程顾问Controller
 */
@Controller
@RequestMapping(value = "${adminPath}/trip/tTrip")
public class TTripController extends BaseController {
	DecimalFormat df2 = new DecimalFormat("#.00");//保留两位小数
	private static final long MILLDAY = 24 * 60 * 60 * 1000l; 
	
	@Autowired
	private TripCounselorService tripCounselorService;
	@Autowired
	private DestinationService destinationService;
	@Autowired
	private CitySceneryService citySceneryService;
	
	@ModelAttribute
	public TripCounselorInfo get(@RequestParam(required = false) String id) {
		TripCounselorInfo entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = tripCounselorService.get(id);
		}
		if (entity == null) {
			entity = new TripCounselorInfo();
		}
		return entity;
	}
	
	@RequestMapping(value = { "list", "" })
	public String list(TripCounselorInfo tripCounselorInfo, HttpServletRequest request, HttpServletResponse response,
			Model model) {
		Page<TripCounselorInfo> page = tripCounselorService.findPage(new Page<TripCounselorInfo>(request, response),tripCounselorInfo);
		model.addAttribute("page", page);
		model.addAttribute("distributionInfo", tripCounselorInfo);
		return "modules/trip/tTripList";
	}

	@RequestMapping(value = "check")
	public String form(TripCounselorInfo tripCounselorInfo,HttpServletRequest request, Model model) throws Exception {
		tripCounselorInfo=tripCounselorService.get(tripCounselorInfo.getTripCounselorId());
		
		String[] name=destinationService.getDestinationList(tripCounselorInfo.getTripCounselorId());
		DestinationInfo d=new DestinationInfo();
		String t = "";//目的地城市名拼接
		for(int j=0;j<name.length;j++){
			d.setDestination(name[j]);
			tripCounselorInfo.getDestination().add(d);
				t=t+name[j];
			if(j<name.length-1){
				t=t+",";
			}	
		}
		tripCounselorInfo.setCountDestination(t);
		
		String[] name2=citySceneryService.getCitySceneryListByTripId(tripCounselorInfo.getTripCounselorId());
		CitySceneryInfo c =new CitySceneryInfo();
		String s= "";
		for(int j=0;j<name2.length;j++){
			c.setSceneryName(name2[j]);
			tripCounselorInfo.getCityScenery().add(c);
			if(name2[j]==null||name2[j].equals("")){
				s="不限";
			}else{
				s=s+name2[j];
			}
			if(j<name.length-1){
				s=s+",";
			}
		}
		tripCounselorInfo.setCountScenery(s);
		
		tripCounselorInfo.setCountPerson(tripCounselorInfo.getChild()+tripCounselorInfo.getPerson());
		tripCounselorInfo.setCountDay(((int) (Math.abs(tripCounselorInfo.getOverTime().getTime()-tripCounselorInfo.getDepartTime().getTime())/MILLDAY))+1);
		
		model.addAttribute("tripCounselorInfo", tripCounselorInfo);
		return "modules/trip/tTripForm";
	}
	
	@RequestMapping(value = "previewList")
	public String previewList(TripCounselorInfo tripCounselorInfo, HttpServletRequest request, HttpServletResponse response,
			Model model) {
		List<TripCounselorInfo> list =tripCounselorService.getTripPreviewList(tripCounselorInfo.getTripCounselorId());
		model.addAttribute("list", list);
		model.addAttribute("tripCounselorId", tripCounselorInfo.getTripCounselorId());
		return "modules/trip/tAddTripList";
	}
	
	@RequestMapping(value = "add")
	public String add(TripCounselorInfo tripCounselorInfo, HttpServletRequest request, HttpServletResponse response,
			Model model) {
		model.addAttribute("tripCounselorId", tripCounselorInfo.getTripCounselorId());
		return "modules/trip/tPreviewAdd";
	}
	
	@RequestMapping(value = "save")
	public String save(TripCounselorInfo tripCounselorInfo, HttpServletRequest request, HttpServletResponse response,
			Model model) {
		int i =tripCounselorService.insertPreview(tripCounselorInfo);
		List<TripCounselorInfo> list =tripCounselorService.getTripPreviewList(tripCounselorInfo.getTripCounselorId());
		model.addAttribute("list", list);
		model.addAttribute("tripCounselorId", tripCounselorInfo.getTripCounselorId());
		return "modules/trip/tAddTripList";
	}

	@RequestMapping(value = "edit")
	public String edit(TripCounselorInfo tripCounselorInfo, HttpServletRequest request, HttpServletResponse response,
			Model model) {
		tripCounselorInfo =tripCounselorService.getPreviewById(tripCounselorInfo.getTripPreviewId());
		model.addAttribute("tripCounselorInfo", tripCounselorInfo);
		model.addAttribute("tripCounselorId", tripCounselorInfo.getTripCounselorId());
		return "modules/trip/tPreviewEdit";
	}
	
	@RequestMapping(value = "update")
	public String update(TripCounselorInfo tripCounselorInfo, HttpServletRequest request, HttpServletResponse response,
			Model model) {
		tripCounselorService.setDisable(tripCounselorInfo.getTripCounselorId());
		int i =tripCounselorService.updateTripPreview(tripCounselorInfo);
		List<TripCounselorInfo> list =tripCounselorService.getTripPreviewList(tripCounselorInfo.getTripCounselorId());
		model.addAttribute("list", list);
		model.addAttribute("tripCounselorId", tripCounselorInfo.getTripCounselorId());
		return "modules/trip/tAddTripList";
	}
	
	@RequestMapping(value = "del")
	public String del(TripCounselorInfo tripCounselorInfo, HttpServletRequest request, HttpServletResponse response,
			Model model) {
		int i =tripCounselorService.deleteById(tripCounselorInfo.getTripPreviewId());
		List<TripCounselorInfo> list =tripCounselorService.getTripPreviewList(tripCounselorInfo.getTripCounselorId());
		model.addAttribute("list", list);
		model.addAttribute("tripCounselorId", tripCounselorInfo.getTripCounselorId());
		return "modules/trip/tAddTripList";
	}
	
	@RequestMapping(value = "sure")
	public String sure(TripCounselorInfo tripCounselorInfo, HttpServletRequest request, HttpServletResponse response,
			Model model) {
		int i =tripCounselorService.setShow(tripCounselorInfo.getTripCounselorId());
		List<TripCounselorInfo> list =tripCounselorService.getTripPreviewList(tripCounselorInfo.getTripCounselorId());
		model.addAttribute("list", list);
		model.addAttribute("tripCounselorId", tripCounselorInfo.getTripCounselorId());
		return "modules/trip/tAddTripList";
	}
	
	@RequestMapping(value = "meals")
	public String meals(MealsInfo info, HttpServletRequest request, HttpServletResponse response,
			Model model) {
		info=tripCounselorService.getMeals();
		model.addAttribute("mealsInfo", info);
		return "modules/trip/tMealsEdit";
	}

	@RequestMapping(value = "saveMeals")
	public String saveMeals(MealsInfo info, HttpServletRequest request, HttpServletResponse response,
			Model model) {
		int i=tripCounselorService.updateMeals(info);
		info=tripCounselorService.getMeals();
		model.addAttribute("mealsInfo", info);
		return "modules/trip/tMealsEdit";
	}
}
